package com.qudian.framework.dao.base;

import com.qudian.pay.core.model.BaseModel;

public class Page extends BaseModel {
	
	private static final long serialVersionUID = -7362171122698408126L;
	
	// 分页标签显示10个页码
	private int showPageCount = 10;
	
	// 每页显示记录数
	private int showCount = 20;
	
	// 总页数
	private int totalPage;
	
	// 总记录数
	private int totalResult;
	
	// 查询 起始rowno
	private int offset;
	
	// 查询 结束rowno
	private int endRowNo;
	
	// 当前页
	private int currentPage;
	
	// 当前记录起始索引
	private int currentResult;
	
	public Page() {
	}
	
	public Page(int currentPage, int showCount) {
		this.currentPage = currentPage;
		this.showCount = showCount;
	}
	
	public int getShowPageCount() {
		return showPageCount;
	}
	
	public void setShowPageCount(int showPageCount) {
		this.showPageCount = showPageCount;
	}
	
	public int getTotalPage() {
		if (totalResult % showCount == 0)
			totalPage = totalResult / showCount;
		else
			totalPage = totalResult / showCount + 1;
		return totalPage;
	}
	
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public int getTotalResult() {
		return totalResult;
	}
	
	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}
	
	public int getCurrentPage() {
		if (currentPage <= 0)
			currentPage = 1;
		if (currentPage > getTotalPage())
			currentPage = getTotalPage();
		return currentPage;
	}
	
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	public int getShowCount() {
		return showCount;
	}
	
	public void setShowCount(int showCount) {
		this.showCount = showCount;
	}
	
	public int getOffset() {
		if (totalResult == 0 || totalResult < (currentPage - 1) * showCount) {
			offset = 0;
			currentPage = 1;
		} else {
			offset = (currentPage - 1) * showCount + 1;
		}
		return offset;
	}
	
	public void setOffset(int offset) {
		this.offset = offset;
	}
	
	public int getEndRowNo() {
		if (totalResult == 0) {
			endRowNo = 0;
		} else {
			endRowNo = currentPage == getTotalPage() ? totalResult : getOffset() - 1 + showCount;
		}
		return endRowNo;
	}
	
	public void setEndRowNo(int endRowNo) {
		this.endRowNo = endRowNo;
	}
	
	public int getCurrentResult() {
		currentResult = (getCurrentPage() - 1) * getShowCount();
		if (currentResult < 0)
			currentResult = 0;
		return currentResult;
	}
	
	public void setCurrentResult(int currentResult) {
		this.currentResult = currentResult;
	}
}
